package com.ml.mall.service.variant.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ml.mall.bean.req.CommonDelReq;
import com.ml.mall.bean.req.variant.VariantRecommendDelReq;
import com.ml.mall.bean.req.variant.VariantRecommendEditReq;
import com.ml.mall.bean.req.variant.VariantRecommendNewReq;
import com.ml.mall.bean.req.variant.VariantRecommendQueryReq;
import com.ml.mall.bean.res.variant.VariantRecommendQueryRes;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.category.SysACategory;
import com.ml.mall.entity.industry.Industry;
import com.ml.mall.entity.variant.SysProductVariantRecommend;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.variant.SysProductVariantMapper;
import com.ml.mall.mapper.variant.SysProductVariantRecommendMapper;
import com.ml.mall.service.variant.SysProductVariantRecommendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* <p>
    *  服务实现类
    * </p>
*
* @author kyyang
* Created 2021-05-31
*/
@Service
@Slf4j
public class SysProductVariantRecommendServiceImpl extends ServiceImpl<SysProductVariantRecommendMapper, SysProductVariantRecommend> implements SysProductVariantRecommendService {

    @Autowired
    private  SysProductVariantRecommendMapper  sysProductVariantRecommendMapper;
    @Autowired
    private SysProductVariantMapper sysProductVariantMapper;

    @Override
    public int variantRecommendNew(VariantRecommendNewReq req) {
        //设置industryId；通过目录查找对应的industryId
        Industry industry =  sysProductVariantMapper.selectIndustryByVariantId(req.getVariantId());
        //先检测对应的推荐是不是大于15个
        if(req.getAdminRecommend() != null && req.getAdminRecommend().equals(1)){
            QueryWrapper<SysProductVariantRecommend> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("admin_recommend",1);
            queryWrapper1.eq("industry_id",industry.getId());
            Integer haveNum = sysProductVariantRecommendMapper.selectCount(queryWrapper1);
            if(haveNum >= CommonConsts.VARIANT_RECOMEND_NUM){
                throw new BizException(CommonEnum.LIMIT_NUM);
            }
        }
        if(req.getPopularRecommend() != null && req.getPopularRecommend().equals(1)){
            QueryWrapper<SysProductVariantRecommend> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("popular_recommend",1);
            queryWrapper1.eq("industry_id",industry.getId());
            if(sysProductVariantRecommendMapper.selectCount(queryWrapper1) >= CommonConsts.VARIANT_RECOMEND_NUM){
                throw new BizException(CommonEnum.LIMIT_NUM);
            }
        }
        if(req.getLatestRecommend() != null && req.getLatestRecommend().equals(1)){
            QueryWrapper<SysProductVariantRecommend> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("latest_recommend",1);
            queryWrapper1.eq("industry_id",industry.getId());
            if(sysProductVariantRecommendMapper.selectCount(queryWrapper1) >= CommonConsts.VARIANT_RECOMEND_NUM){
                throw new BizException(CommonEnum.LIMIT_NUM);
            }
        }
        log.info("req======={}",req);
        SysProductVariantRecommend sysProductVariantRecommend = new SysProductVariantRecommend();
        BeanUtils.copyProperties(req,sysProductVariantRecommend);
        //查询是不是已经存在了这个卡；如果存在直接更新；不存在就创建
        QueryWrapper<SysProductVariantRecommend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("variant_id",sysProductVariantRecommend.getVariantId());
        SysProductVariantRecommend queryObj = sysProductVariantRecommendMapper.selectOne(queryWrapper);
        //如果推荐信息已被逻辑删除，则先将旧的数据清理，防止数据冲突
        sysProductVariantRecommendMapper.cleanOne(sysProductVariantRecommend.getVariantId());
        if(queryObj != null){
            if(req.getAdminRecommend() != null && req.getAdminRecommend().equals(1)){
                queryObj.setAdminRecommend(1);
            }
            if(req.getPopularRecommend() != null && req.getPopularRecommend().equals(1)){
                queryObj.setPopularRecommend(1);
            }
            if(req.getLatestRecommend() != null && req.getLatestRecommend().equals(1)){
                queryObj.setLatestRecommend(1);
            }
            return sysProductVariantRecommendMapper.update(queryObj, queryWrapper);
        }
        sysProductVariantRecommend.setSortNo(1);              // 新增推荐默认等于1
        sysProductVariantRecommend.setIndustryId(industry.getId());
        sysProductVariantRecommendMapper.insert(sysProductVariantRecommend);
        return 0;
    }

    @Override
    public int variantRecommendDel(VariantRecommendDelReq req) {
        //没有指定删除类型时直接返回
        if(!CommonConsts.FIXED_NUM_ZERO.equals(req.getLatestRecommend())
            && !CommonConsts.FIXED_NUM_ZERO.equals(req.getAdminRecommend())
            && !CommonConsts.FIXED_NUM_ZERO.equals(req.getPopularRecommend())){
            return 0;
        }

        //去除指定类型的推荐信息 1->0
        LambdaUpdateWrapper<SysProductVariantRecommend> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CommonConsts.FIXED_NUM_ZERO.equals(req.getLatestRecommend()), SysProductVariantRecommend::getLatestRecommend, 0);
        updateWrapper.set(CommonConsts.FIXED_NUM_ZERO.equals(req.getAdminRecommend()), SysProductVariantRecommend::getAdminRecommend,0);
        updateWrapper.set(CommonConsts.FIXED_NUM_ZERO.equals(req.getPopularRecommend()), SysProductVariantRecommend::getPopularRecommend, 0);
        updateWrapper.eq(SysProductVariantRecommend::getId, req.getId());
        return  sysProductVariantRecommendMapper.update(null, updateWrapper);
    }

    @Override
    public int variantRecommendEdit(VariantRecommendEditReq req) {
        SysProductVariantRecommend sysProductVariantRecommend = new SysProductVariantRecommend();
        sysProductVariantRecommend.setId(req.getId());
        sysProductVariantRecommend.setSortNo(req.getSortNo());
        return sysProductVariantRecommendMapper.updateById(sysProductVariantRecommend);
    }

    @Override
    public List<VariantRecommendQueryRes> variantRecommendQueryAll(VariantRecommendQueryReq req) {
        return sysProductVariantRecommendMapper.queryAllRecommend(req);
    }


}
